package renguanyu.app.auth4fx.dao.permission;

import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import renguanyu.app.auth4fx.util.DbUtil;
import renguanyu.app.auth4fx.util.Util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PermissionDao {

    public static Integer getPermissionTotal(String name) {
        StringBuilder sql = new StringBuilder();
        List<Object> params = new ArrayList<>();
        sql.append("""
        select
        count(*) as total
        from t_permission
        where deleted = 'N'
        """);
        if (!name.isBlank()) {
            sql.append("""
            and name like ?
            """);
            params.add(name);
        }
        return DbUtil.query(sql.toString(), new ScalarHandler<>(), params.toArray());
    }

    public static List<Map<String, Object>> getPagedPermissions(Integer limit, Integer offset, String name) {
        StringBuilder sql = new StringBuilder();
        List<Object> params = new ArrayList<>();
        sql.append("""
        select *
        from t_permission
        where deleted = 'N'
        """);
        if (!name.isBlank()) {
            sql.append("""
            and name like ?
            """);
            params.add(name);
        }
        sql.append("""
        order by updated_at desc
        """);
        sql.append("""
        limit ? offset ?
        """);
        params.add(limit);
        params.add(offset);
        return DbUtil.query(sql.toString(), new MapListHandler(), params.toArray());
    }

    public static Integer savePermission(String name){
        String sql = """
        INSERT INTO t_permission
        (id, deleted, created_by, created_at, updated_by, updated_at, name)
        VALUES(?, ?, ?, ?, ?, ?, ?)
        """;
        List<Object> params = new ArrayList<>();
        params.add(Util.uuid());
        params.add("N");
        params.add(Util.getLoginName());
        params.add(Util.getCurrentDateTimeString());
        params.add(Util.getLoginName());
        params.add(Util.getCurrentDateTimeString());
        params.add(name);
        return DbUtil.update(sql, params.toArray());
    }

    public static Integer updatePermission(String name,String id){
        String sql = """
        UPDATE t_permission
        SET updated_by=?, updated_at=?, name=?
        WHERE id=?
        """;
        List<Object> params = new ArrayList<>();
        params.add(Util.getLoginName());
        params.add(Util.getCurrentDateTimeString());
        params.add(name);
        params.add(id);
        return DbUtil.update(sql, params.toArray());
    }

    public static Integer deletePermission(List<String> ids, String inSql) {
        String sql = "UPDATE t_permission SET deleted=?, updated_by=?, updated_at=? WHERE id IN (" + inSql + ")";
        List<Object> params = new ArrayList<>();
        params.add("Y");
        params.add(Util.getLoginName());
        params.add(Util.getCurrentDateTimeString());
        params.addAll(ids);
        return DbUtil.update(sql, params.toArray());
    }
}
